package com.situ.stmall.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.situ.stmall.entity.Address;
import com.situ.stmall.entity.Order;
import com.situ.stmall.entity.User;
import com.situ.stmall.quartz.QuartzManager;
import com.situ.stmall.service.AddressService;
import com.situ.stmall.service.OrderService;
import com.situ.stmall.service.ShopcartService;

@Controller
@RequestMapping("/user/order")
public class OrderController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private ShopcartService shopcartService;
	@Autowired
	private QuartzManager quartzManager;
	

	/**
	 * 确认订单
	 * @param cartIds
	 * @param session
	 * @param model
	 * @return
	 */
	@PostMapping("/confirm")
	public String confirm(@RequestParam("cartId") Integer[] cartIds,
			HttpSession session,
			Model model) {
		
		// 查询地址// 取出用户的ID
		User user = (User) session.getAttribute("user");
		// 查询当前登录用户的地址信息
		List addresses = addressService.getByUserId(user.getId());
		model.addAttribute("addresses", addresses);
		
		// 查询购物车信息
		List carts;
		try {
			carts = shopcartService.getByIds(user.getId(), cartIds);
			model.addAttribute("carts", carts);
			
			return "user/orderConfirm";
			
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("error", e.getMessage());
			return "user/error";
		}
		
		
	}
	/**
	 * 提交订单
	 */
	@GetMapping("/add")
	public String add(@RequestParam("cartId") Integer[] cartIds,
			Integer addressId,
			HttpSession session,
			Model model) {
		// 取出用户的ID
		User user = (User) session.getAttribute("user");
		
		// 调用service层
		try {
			Order order = orderService.add(user.getId(), cartIds, addressId);
			
			// 启动Job, 让job在30分钟后执行, 
			quartzManager.addJob(order.getId());

			// 订单生成成功, 跳转支付
			return "redirect:/user/order/pay?orderId="+order.getId();
			
		} catch (Exception e) {
			e.printStackTrace();
			
			model.addAttribute("error", e.getMessage());
			// 跳转到一个错误页面
			return "user/error";
		}
	}
	
	/**
	 * 支付页面
	 */
	@GetMapping("/pay")
	public String pay(String orderId, Integer addressId,
			Model model) {
		//  根据订单ID, 查询订单信息
		Order order = orderService.getById(orderId);
				
		if (addressId != null) {
			// 将收货地址写入数据库中
			order.setAddressId(addressId);
			orderService.update(order);	
			order = orderService.getById(orderId);
		}
		
		// 再利用model传递到pay页面中, 
		model.addAttribute("order", order);
		
		return "user/pay";
	}
	
	@PostMapping("/pay")
	public String pay(String id, String paypwd,
			HttpSession session,
			Model model) {
		User user = (User) session.getAttribute("user");
		
		try {
			int result = orderService.pay(id, paypwd, user.getId());
			// 支付成功
			// 跳转页面-> 订单列表
			return "redirect:/user/order/list";
		} catch (Exception e) {
			e.printStackTrace();
			// 支付失败
			model.addAttribute("error", e.getMessage());
			return "user/error";
		}
	}
	
	@GetMapping("/list")
	public String list() {
		return "user/orderList";
	}
}




